import { PasswordValidationException } from '../app/exception/HttpException'
import bcrypt from 'bcryptjs'

// 辅助函数：生成随机用户名
export const generateRandomUsername = () => {
  const prefix = 'user_'
  const randomPart = Math.random().toString(36).substr(2, 6)
  return prefix + randomPart
}

// 加密密码方法
export const hashPassword = (decryptedPassword: string) => {
  if (!decryptedPassword) {
    throw new PasswordValidationException('', '密码不能为空！')
  }
  // 加盐
  const salt = bcrypt.genSaltSync(10)
  //hash保存的是密文
  const hash = bcrypt.hashSync(decryptedPassword, salt)

  console.log(hash, '加密后的password')

  return hash
}
